/******************************************************************************
*@file  : app.c
*@brief : SPI Master Demos 
******************************************************************************/
#include  "app.h"


#define BUFFER_LENGTH    (256)

uint8_t TxBuffer[BUFFER_LENGTH];
uint8_t RxBuffer[BUFFER_LENGTH];

SPI_HandleTypeDef SPI_Handle;
uint32_t DataLength = 128;
uint32_t test_cnt = 0;
void printf_buff_byte(uint8_t* buff, uint32_t length)
{
	uint32_t i;

	for(i = 0; i < length; i++)
	{
		printf("%.2x ",buff[i]);	
	}
	printf("\n");
}

void SPI_Init(void)
{
    SPI_Handle.Instance                 = SPI1;
    SPI_Handle.Init.SPI_Mode            = SPI_MODE_MASTER;
    SPI_Handle.Init.SPI_Work_Mode       = SPI_WORK_MODE_0;
    SPI_Handle.Init.X_Mode              = SPI_1X_MODE;
    SPI_Handle.Init.First_Bit           = SPI_FIRSTBIT_MSB;
    SPI_Handle.Init.BaudRate_Prescaler  = SPI_BAUDRATE_PRESCALER_8;
    HAL_SPI_Init(&SPI_Handle);
}

void SPI_Master_Comm_Test(void)
{
    uint32_t i;
    
    uint32_t COM_OK  = 0;
    uint32_t COM_Err = 0;
    
    printfS("SPI Master Comm Demo. (should connect to another board running SPI Slave Comm Demo, & Slave should run first)\r\n\r\n");

    SPI_Init();
    
    //printfS("Please press the USR_PB button to continue......\r\n\r\n");
    
    //while(GPIO_PIN_SET == BSP_PB_GetState());
    
    do
    {        
        for (i = 0; i < DataLength; i++)
        {
            TxBuffer[i] = 0 + i;
        }

        HAL_SPI_Transmit(&SPI_Handle, TxBuffer, DataLength, 0);        
        HAL_Delay(300);
        HAL_SPI_Receive(&SPI_Handle, RxBuffer, DataLength, 0);
        
        for (i = 0; i < DataLength; i++)
        {
            if (TxBuffer[i] != RxBuffer[i]) 
            {                
                COM_Err++;
                printfS("Error@ %x\n",i);
                goto end;
            }
            else 
            {
                COM_OK++;
            }
        }
        
        printfS("SPI Master Test OK count %d times \r\n",  COM_OK);
        printfS("SPI Master Test Err count %d times \r\n", COM_Err);

        COM_OK = 0;
        COM_Err = 0;
        
        //while(GPIO_PIN_SET == BSP_PB_GetState());
        HAL_Delay(300);
    }while(1);
end:
    printf_buff_byte(RxBuffer, DataLength);
    printfS("SPI Master Test End!!! \r\n");
}


void SPI_Master_Comm_IT_Test(void)
{
    volatile uint32_t i;
    
    uint32_t COM_OK  = 0;
    uint32_t COM_Err = 0;
        
    SPI_Init();
    
    
    

    do
    {      

        for (i = 0; i < DataLength; i++)
        {
            TxBuffer[i] = test_cnt + i;
        }
        test_cnt++;
        HAL_SPI_Transmit_IT(&SPI_Handle, TxBuffer, DataLength);
        while(SPI_Handle.TxState != SPI_TX_STATE_IDLE);     
        HAL_Delay(300);
        memset(RxBuffer, 0, DataLength);
        HAL_SPI_Receive_IT(&SPI_Handle, RxBuffer, DataLength);
        while(SPI_Handle.RxState != SPI_RX_STATE_IDLE);

        i = 0;
        for (i = 0; i < DataLength; i++)
        {
            if (TxBuffer[i] != RxBuffer[i]) 
            {                
                COM_Err++;
                printfS("Error@ %x\n",i);
                printf_buff_byte(RxBuffer, DataLength);;//goto end;
            }
            else 
            {
                COM_OK++;
            }
        }
        
        printfS("SPI Master IT Test OK count %d times \r\n",  COM_OK);
        printfS("SPI Master IT Test Err count %d times \r\n", COM_Err);

        COM_OK = 0;
        COM_Err = 0;
        
        //while(GPIO_PIN_SET == BSP_PB_GetState());
        HAL_Delay(300);
    }while(1);
end:
    printf_buff_byte(RxBuffer, DataLength);
    printfS("SPI Master Test End!!! \r\n");
}


void SPI_Master_FullDuplex_Test(void)
{
    uint32_t i;
    
    uint32_t lu32_COM_OK  = 0;
    uint32_t lu32_COM_Err = 0;
    uint32_t lu32_DataLength = 128;

    printfS("SPI Master Full Duplex Test is Ready!!! \r\n");

    SPI_Init();
        
    do
    {
        for (i = 0; i < BUFFER_LENGTH; i++)
        {
            TxBuffer[i] = i;
        }
        
        HAL_SPI_TransmitReceive(&SPI_Handle, TxBuffer, RxBuffer, lu32_DataLength, 0);
        
        for (i = 0; i < lu32_DataLength; i++)
        {
            if (TxBuffer[i] != (RxBuffer[i]-1)) 
            {
                printfS("There is one mistake : TxBuffer[%d]: 0x%02x != RxBuffer[%d]: 0x%02x \r\n", i,TxBuffer[i], i, RxBuffer[i]);
                
                lu32_COM_Err++;
            }
            else 
            {
                lu32_COM_OK++;
            }
        }
        
        printfS("SPI Master Full Duplex Test OK count %d times \r\n",  lu32_COM_OK);
        printfS("SPI Master Full Duplex Test Err count %d times \r\n", lu32_COM_Err);
        
        lu32_COM_OK = 0;
        lu32_COM_Err = 0;

//        while(false == UserKEY_Get());
        HAL_Delay(500);
    }while(1);
    
    printfS("SPI Master Full Duplex Test End!!! \r\n");
}

/************************For SPI NorFlash Test***************************/ 

#define BUFFER_LENGTH_NOR    (1024)

uint8_t TxBuffer_Nor[BUFFER_LENGTH_NOR];
uint8_t RxBuffer_Nor[BUFFER_LENGTH_NOR];

/******************************************************************************
*@brief : SPI Nor Flash Demo
*         
*@param : None
*@return: None
******************************************************************************/
void SPI_Nor_Flash_Test(void)
{
    uint32_t i;
    
    uint16_t id;
    uint32_t ErrCount = 0;
    uint32_t TestMode = 0;
    
    GPIO_InitTypeDef GPIO_Handle = {0}; 
    
    #define TEST_1X_MODE    (0)
    #define TEST_2X_MODE    (2)
    #define TEST_4X_MODE    (3)
    
    printfS("SPI Nor Flash RW Demo(should connect to a NOR Flash. eg. W25Q32B)\r\n\r\n");

    IC_W25Qxx_Init(SPI2);
    
    printfS("Please press the USR_PB button to continue......\r\n\r\n");
    
    //while(GPIO_PIN_SET == BSP_PB_GetState());
    
    HAL_Delay(500);
    
    /* Please Select Test Mode */
    TestMode = TEST_1X_MODE;

    /*************************************** Test Prepare ***************************************/
    if (TestMode == TEST_1X_MODE) 
        printfS("SPI TEST_1X_MODE is Start!!! \r\n");
    else if (TestMode == TEST_2X_MODE) 
        printfS("SPI TEST_2X_MODE is Start!!! \r\n");
    else if (TestMode == TEST_4X_MODE) 
        printfS("SPI TEST_4X_MODE is Start!!! \r\n");

    /* Read Read Manufacture ID and Device ID */
    id = IC_W25Qxx_Read_ID();

    printfS("Get Manufacture ID and Device ID : 0x%04X \r\n", id);

    /* Erase Chip */
    IC_W25Qxx_EraseChip();
    
    IC_W25Qxx_Read_Data(RxBuffer_Nor, 0, BUFFER_LENGTH_NOR);

    for (i = 0; i < BUFFER_LENGTH_NOR; i++)
    {
        if (RxBuffer_Nor[i] != 0xFF) 
        {
            ErrCount++;
        } 
    }

    if (ErrCount) 
        printfS("Erase Chip Fail!!! \r\n");
    else 
        printfS("Erase Chip Success!!! \r\n");

    /* Clear Error Count */
    ErrCount = 0;
    
    for (i = 0; i < BUFFER_LENGTH_NOR; i++)
    {
        TxBuffer_Nor[i] = i;
    }
    
    /************************************* Test Prepare End **************************************/
    
    switch (TestMode)
    {
        case TEST_1X_MODE: 
        {
            IC_W25Qxx_PageProgram(TxBuffer_Nor, 0,   256);
            IC_W25Qxx_PageProgram(TxBuffer_Nor, 256, 256);
            IC_W25Qxx_PageProgram(TxBuffer_Nor, 512, 256);
            IC_W25Qxx_PageProgram(TxBuffer_Nor, 768, 256);
            
            IC_W25Qxx_Read_Data(RxBuffer_Nor, 0, BUFFER_LENGTH_NOR);
            
            for (i = 0; i < BUFFER_LENGTH_NOR; i++)
            {
                if (TxBuffer_Nor[i] != RxBuffer_Nor[i]) 
                {
                    ErrCount++;
                } 
            }
            
            if (ErrCount) 
                printfS("SPI TEST_1X_MODE is Fail!!! \r\n");
            else 
                printfS("SPI TEST_1X_MODE is Success!!! \r\n");
        }break;

        case TEST_2X_MODE: 
        {
            IC_W25Qxx_PageProgram(TxBuffer_Nor, 0,   256);
            IC_W25Qxx_PageProgram(TxBuffer_Nor, 256, 256);
            IC_W25Qxx_PageProgram(TxBuffer_Nor, 512, 256);
            IC_W25Qxx_PageProgram(TxBuffer_Nor, 768, 256);
            
            IC_W25Qxx_Read_Dual_Output(RxBuffer_Nor, 0, BUFFER_LENGTH_NOR);
            
            for (i = 0; i < BUFFER_LENGTH_NOR; i++)
            {
                if (TxBuffer_Nor[i] != RxBuffer_Nor[i]) 
                {
                    ErrCount++;
                } 
            }

            if (ErrCount) 
                printfS("SPI TEST_2X_MODE is Fail!!! \r\n");
            else 
                printfS("SPI TEST_2X_MODE is Success!!! \r\n");
        }break;

        case TEST_4X_MODE: 
        {
            IC_W25Qxx_QuadConfig(true);
            
            IC_W25Qxx_PageProgram_Quad(TxBuffer_Nor, 0,   256);
            IC_W25Qxx_PageProgram_Quad(TxBuffer_Nor, 256, 256);
            IC_W25Qxx_PageProgram_Quad(TxBuffer_Nor, 512, 256);
            IC_W25Qxx_PageProgram_Quad(TxBuffer_Nor, 768, 256);
            
            IC_W25Qxx_Read_Quad_Output(RxBuffer_Nor, 0, BUFFER_LENGTH_NOR);
            
            for (i = 0; i < BUFFER_LENGTH_NOR; i++)
            {
                if (TxBuffer_Nor[i] != RxBuffer_Nor[i]) 
                {
                    ErrCount++;
                } 
            }

            if (ErrCount) 
                printfS("SPI TEST_4X_MODE is Fail!!! \r\n");
            else 
                printfS("SPI TEST_4X_MODE is Success!!! \r\n");

            IC_W25Qxx_QuadConfig(false);
        }break;

        default: break; 
    }
}

void SPI_Master_Test(enum_TEST_MODE_t fe_Mode)
{
    switch (fe_Mode)
    {
        case TEST_MASTER_COMM_LOOP: 
            SPI_Master_Comm_Test();
            break; 
        
        case TSET_MASTER_COMM_IT:
            SPI_Master_Comm_IT_Test();
            break;
        
        case TEST_MASTER_FULLDUPLEX:
            SPI_Master_FullDuplex_Test();
            break;
      
        case TEST_MASTER_NOR_FLASH: 
            SPI_Nor_Flash_Test();
            break;

        default: break;     
    }
}

